<?php

/**
 * 后台专题控制器
 */
class SpecialController extends AdminBase {

    public function actionAdd() {
        $objModel = new SpecialModel('add');
        if (IS_POST) {
            $arrData = $objModel->saveImg();
            $_POST['SpecialModel'] = CMap::mergeArray($_POST['SpecialModel'], $arrData);            
            $_POST = $_POST['SpecialModel'];
            $this->baseAdd($objModel, 'index');
        } else {
            $this->render('add', array('model' => $objModel));
        }
    }

    public function actionEdit() {
        $objModel = SpecialModel::model()->findByPk(Yii::app()->request->getParam('id'));        
        if (IS_POST) {
            $arrData = $objModel->saveImg();            
            $_POST['SpecialModel'] = CMap::mergeArray($_POST['SpecialModel'], $arrData);             
            $_POST = $_POST['SpecialModel'];
            $attributes_old = $objModel->attributes;
            $objModel->attributes = $_POST;
            if ($objModel->save()) {
                //把原来的删除掉/包括附件
                $sitefileurl = ConfigModel::model()->getConfig('sitefileurl');
                foreach($arrData as $key => $val){
                    if($attributes_old[$key]){
                        $path = str_replace($sitefileurl, '', $attributes_old[$key]);
                        $aid = AttachmentModel::model()->find(array('condition' => 'filepath = :filepath', 'params' => array('filepath' => $path)));
                        if($aid){
                            $aid = $aid->aid;
                            AttachmentModel::model()->deleteFileByAid($aid);
                        }
                    }        
                }                    
                $this->success('修改成功！', self::U($url));
            } else {
                $error = $objModel->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        }
        $this->render('edit', array('model' => $objModel));
    }

    public function actionDelete() {
        $this->baseDelete(SpecialModel::model(), 'index');
    }

    public function actionIndex() {
        $objModel = new SpecialModel('search');
        $objModel->resetAttributes();

        /*
          if(isset($_GET['SpecialModel']))
          {
          $objModel->attributes = $_GET['SpecialModel'];
          }
         */
        $objCriteria = $objModel->createSearchCriteria();

        //使用后台分页 
        $intCount = SpecialModel::model()->count($objCriteria);
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;
        $page = self::page($intCount, $limit, $pageId);
        $strPage = $page->show();
        $objCriteria->limit = $page->listRows;
        $objCriteria->offset = $page->firstRow;

        //排序
        $objSort = new CSort('SpecialModel');
        $objSort->defaultOrder = 'id DESC';
        $objSort->applyOrder($objCriteria);

        $objModels = SpecialModel::model()->findAll($objCriteria);

        $this->assign('pages', $strPage);
        $this->assign('model', $objModel);
        $this->assign('count', $intCount);
        $this->assign('models', $objModels);

        $this->render('index');
    }

}
